<?php

/**
 * @copyright       : FOSP
 * @version         : 20120409
 * @name            : Fpt_Business_MatchExtend_Adapter_Mysql
 * @todo            : MySQL access table match_extend
 * @author          : TrongNV
 */
class Fpt_Business_MatchExtend_Adapter_Mysql extends Fpt_Business_MatchExtend_Adapter_Abstract {

    /**
     * Get Fpt_Business_MatchExtend_Adapter_Mysql's instance
     * @return Fpt_Business_MatchExtend_Adapter_Mysql
     */
    public static function getInstance() {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get detail match extend by code
     * @param array $strMatchCode
     * @return array|bool
     * @author TrongNV
     */
    public function getDetailMatchExtendByCode($strMatchCode) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_getMatchExtendByCode(:p_matchcode);');

            // Bind param
            $stmt->bindParam('p_matchcode', $strMatchCode);

            // Execute
            $stmt->execute();
            
            // Get result
            $result = array();

            // Get format data
            $formatInstance = new Fpt_Business_MatchExtend_Metadata_MatchExtend();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $formatInstance->init($row)->getFormatedData());

            // Close
            $stmt->closeCursor();
            
            // Release variables
            unset($stmt, $strMatchCode, $formatInstance);

            //Return array
            return $result;
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get detail match extend by ids
     * @param array $arrMatchExtendIDs
     * @return array|bool
     * @author TrongNV
     */
    public function getDetailMatchExtendByIDs($intMatchID) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_getDetailMatchExtend(:p_matchid);');

            // Bind param
            $stmt->bindParam('p_matchid', $intMatchID, PDO::PARAM_INT);

            // Execute
            $stmt->execute();
            
            // Get result
            $result = array();

            // Get format data
            $formatInstance = new Fpt_Business_MatchExtend_Metadata_MatchExtend();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $formatInstance->init($row)->getFormatedData());
            
            // Close
            $stmt->closeCursor();
            // Release variables
            unset($stmt, $intMatchID, $formatInstance);

            return $result;
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Insert match predict
     * @param array $arrMatchPredict
     * @return bool
     * @author TrongNV
     */
    public function insertMatchPredict($params) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL  sp_subpage_insertPredictMatch(:p_matchid, :p_mobile, :p_userid, :p_goalteam1, :p_goalteam2, :p_numpeople, :p_codepre);');
            // Bind param
            $stmt->bindParam('p_matchid', $params['match_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_mobile', $params['mobile']);
            $stmt->bindParam('p_userid', $params['userid'], PDO::PARAM_INT);
            $stmt->bindParam('p_goalteam1', $params['goal_team1'], PDO::PARAM_INT);
            $stmt->bindParam('p_goalteam2', $params['goal_team2'], PDO::PARAM_INT);
            $stmt->bindParam('p_numpeople', $params['num_people'], PDO::PARAM_INT);
            $stmt->bindParam('p_codepre', $params['code_predict']);
            $stmt->execute();
            $result = $stmt->fetch();
            $stmt->closeCursor();
            //Return
            return $result['result'];
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Update match predict ( send_sms )
     * @param $intMatchID, $strMobile, $strCodePredict
     * @return bool
     * @author TrongNV
     */
    public function updateMatchPredict($intMatchID, $strMobile, $strCodePredict) {
         try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_setSMSPredictMatch(:p_matchid, :p_mobile, :p_codepredict);');
            // Bind param
            $stmt->bindParam('p_matchid', $intMatchID, PDO::PARAM_INT);
            $stmt->bindParam('p_mobile', $strMobile);
            $stmt->bindParam('p_codepredict', $strCodePredict);
            $stmt->execute();
            $result = $stmt->fetch();
            $stmt->closeCursor();
            //Return
            return $result['result'];
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    public function insertCommentMatchExtend($params) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_insertComments(:p_userid, :p_matchid, :p_email, :p_fullname, :p_teamid, :p_content, @p_result);');
            // Bind param
            $stmt->bindParam('p_userid', $params['userid']);
            $stmt->bindParam('p_matchid', $params['match_id']);
            $stmt->bindParam('p_email', $params['email']);
            $stmt->bindParam('p_fullname', $params['fullname']);
            $stmt->bindParam('p_teamid', $params['team_id']);
            $stmt->bindParam('p_content', $params['content']);
            $stmt->execute();
            $stmt->closeCursor();
            $stmt = $dbObj->query("SELECT @p_result");

            $rs = $stmt->fetchColumn();

            // Release
            unset($stmt);
            return $rs;
        } catch (Exception $ex) {
            echo $ex->getMessage();
            exit();
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get list comment by matchid and teamid
     * @param int $match_id, int $team_id
     * @return array|bool
     * @author TrongNV
     */
    public function getCommentByMatch($intMatchID, $intTeamID, $strIsApproved) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_getCommentByMatch(:p_matchid, :p_teamid, :p_isapprove);');

            // Bind param
            $stmt->bindParam('p_matchid', $intMatchID, PDO::PARAM_INT);
            $stmt->bindParam('p_teamid', $intTeamID, PDO::PARAM_INT);
            $stmt->bindParam('p_isapprove', $strIsApproved);

            // Execute
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();

            //Return array
            return $result;
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get report match predict
     * @param int $match_id
     * @return array|bool
     * @author TrongNV
     */
    public function getReportMatchPredict($intMatchID, $intType) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_getReportPredictMatch(:p_matchid, :p_resulttype);');

            // Bind param
            $stmt->bindParam('p_matchid', $intMatchID, PDO::PARAM_INT);
            $stmt->bindParam('p_resulttype', $intType, PDO::PARAM_INT);

            // Execute
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();

            //Return array
            return $result;
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get total match predict
     * @param int $match_id
     * @return array|bool
     * @author TrongNV
     */
    public function getTotalMatchPredict($intMatchID) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_getTotalPredictMatch(:p_matchid);');

            // Bind param
            $stmt->bindParam('p_matchid', $intMatchID, PDO::PARAM_INT);

            // Execute
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();

            //Return array
            return $result;
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get top match predict
     * @param int $match_id and $limit
     * @return array|bool
     * @author TrongNV
     */
    public function getTopMatchPredict($intMatchID, $limit) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_getTopPreditionMatch(:p_matchid, :p_limit);');

            // Bind param
            $stmt->bindParam('p_matchid', $intMatchID, PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);

            // Execute
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();

            //Return array
            return $result;
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }
    /**
     * Insert SMS
     * @param type $params
     * @return boolean 
     */
     public function insertSmsPredict($params) {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSubpageDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_subpage_insertSMSMessage(:p_moid , :p_telco, :p_servicenum , :p_phone , :p_syntax , :p_message , :p_return, :p_messagereturn , :p_numpeople , :p_codepredict, @p_result);');
            
            // Bind param
            $stmt->bindParam('p_moid', $params['moid']);
            $stmt->bindParam('p_telco', $params['telco']);
            $stmt->bindParam('p_servicenum', $params['service_num']);
            $stmt->bindParam('p_phone', $params['phone']);
            $stmt->bindParam('p_syntax', $params['syntax']);
            $stmt->bindParam('p_message', $params['message']);
            $stmt->bindParam('p_return', $params['return']);
            $stmt->bindParam('p_messagereturn', $params['message_return']);
            $stmt->bindParam('p_numpeople', $params['num_people']);
            $stmt->bindParam('p_codepredict', $params['code_predict']);
            $stmt->execute();
            $stmt->closeCursor();
            $stmt = $dbObj->query("SELECT @p_result");

            $rs = $stmt->fetchColumn();

            // Release
            unset($stmt);
            return $rs;
        } catch (Exception $ex) {
            return $ex->getMessage();
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}